﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SequenceAssembler
{
    class Program
    {
		static void Main(string[] args)
		{
			bool createNewGraph = true;
			bool hasMerged = false;
			bool hasTipRemoved = false;
			bool hasTinyRepeatResolved = false;
			bool hasLowCoverageRemoved = false;
			bool reportIntermediate = false;
            bool createTransformFile = true;
            short kmerLen = 21;

            if (createTransformFile == true)
            {
                ReadFile readFile = new ReadFile();
                //readFile.Filename = "../../../data/test_reads.fa";
                readFile.Filename = @"E:\zhaosun\workspace\sequenceLab\data\s_2_whole_sequence_fasta_len.txt";
                readFile.InsertLength = 100;
                readFile.InsertVariance = 25.0F;
                ReadFile[] readFiles = { readFile };
                string transformFilePath = @"E:\zhaosun\workspace\sequenceLab\transformData\s_2_whole_sequence_fasta_len.transform.txt";
                string categoryFilePath = @"E:\zhaosun\workspace\sequenceLab\transformData\s_2_whole_sequence_fasta_len.categorytxt";
                SequenceTransformer transformer = new SequenceTransformer(kmerLen, readFiles, transformFilePath, categoryFilePath);
                transformer.Transform();
                return;
            }

			using (BackGraph bg = new BackGraph(kmerLen))
			{
				if (createNewGraph)
				{
					//no load, create a new one and save.
					ReadFile readFile = new ReadFile();
					//readFile.Filename = "../../../data/test_reads.fa";
                    readFile.Filename = @"E:\zhaosun\workspace\sequenceLab\data\s_2_whole_sequence_fasta_len.txt";
					readFile.InsertLength = 100;
					readFile.InsertVariance = 25.0F;
					ReadFile[] readFiles = { readFile };
					int abundanceThreshold = 1;
					short multiplicityThreshold = 0;
                    
					DeBruijnGraphBuilder builder = new DeBruijnGraphBuilder(bg, abundanceThreshold, multiplicityThreshold);
					builder.Build(readFiles);
				}
				else
				{
					bg.Load();
				}

				if (!hasMerged)
				{
					SimpleCellMerger merger = new SimpleCellMerger(bg);
					Console.WriteLine("Before iterative merge cell count: {0}", bg.CellCount);
					merger.Merge();
					Console.WriteLine("After iterative merge cell count: {0}", bg.CellCount);
				}

				if (!hasTipRemoved)
				{
					TipRemover remover = new TipRemover(bg);
					Console.WriteLine("Before iterative remove cell count: {0}", bg.CellCount);
					remover.Remove();
					Console.WriteLine("After iterative remove cell count: {0}", bg.CellCount);
				}

				if (!hasTinyRepeatResolved) {
					TinyRepeatResolver resolver = new TinyRepeatResolver(bg);
					Console.WriteLine("Before iterative tiny resolve cell count: {0}", bg.CellCount);
					resolver.Resolve();
					Console.WriteLine("After iterative tiny resolve cell count: {0}", bg.CellCount);
				}

				if (!hasLowCoverageRemoved) {
					LowCoverageRemover cvremover = new LowCoverageRemover(bg);
					cvremover.CoverageThreshold = 3.0F;
					Console.WriteLine("Before iterative cvremove cell count: {0}", bg.CellCount);
					cvremover.Remove();
					Console.WriteLine("After iterative cvremove cell count: {0}", bg.CellCount);
				}

				if (reportIntermediate) {
					IntermediateResultReporter reporter = new IntermediateResultReporter(bg);
					reporter.Report();
				}
			}
		}
    }
}
